unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  HTTPApp, IdHTTP, XMLDoc, XMLIntf, FMX.Objects, RegularExpressions,
  FMX.ListView.Types, FMX.ListView, Androidapi.JNI.GraphicsContentViewText,
  FMX.Helpers.Android;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Text1: TText;
    ListView1: TListView;
    Checkbox_Youtubed: TCheckBox;
    Checkbox_Buy: TCheckBox;
    Checkbox_Maybe: TCheckBox;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ListView1Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Refresh(Sender: TObject);
    procedure Checkbox_YoutubedChange(Sender: TObject);
    procedure Checkbox_BuyChange(Sender: TObject);
    procedure Checkbox_MaybeChange(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

  MyThread = class(TThread)
  private
    { Private-Deklarationen }
  protected
    procedure Execute; override;
  end;

  TStringArray = Array of String;

  TSongItem = record
    sArtistTrack : String;
    bYoutoubed, bBuy, bMaybeBuy, bBought : boolean;
  end;


var
  Form1: TForm1;
  sHttpText: String;
  AoT_SongItems: Array of TSongItem;


implementation

{$R *.fmx}


procedure MyThread.Execute;
var
  HTTP: TIdHTTP;
begin
  HTTP := TIdHTTP.Create;
  sHttpText := HTTP.get('http://www.housetime.fm/releases/house-elektro/');
//  sHttpText := '<th style="width:100%"><a href="/release/john-martin/24744/anywhere-for-you/">&nbsp;John Martin - Anywhere For You</a>';
  HTTP.Free;
end;

function extractDataFromHTMl(sHTML: String) : TStringArray;
var
  match : TMatch;
  matches: TMatchCollection;
  regex : String;
  i : integer;

begin
  regex := '[/]["][>][&](nbsp;).*(</a>)';

  matches := TRegEx.Matches(sHTML, regex);

  if matches.Count > 0 then
  begin
  Form1.Text1.Text := 'in schleife';
  SetLength(Result,matches.Count);
    for i:=0 to matches.Count-1 do
    begin
       Result[i] := matches.Item[i].Value;
    end;
  end;
  Form1.Text1.Text := 'idle';
end;


procedure TForm1.Button1Click(Sender: TObject);

var
  Thread: MyThread;
  AoS_Namen: TStringArray;
  sTemp: String;
  i : integer;
  LItem: TListViewItem;

begin
  Form1.Text1.Text := 'Loading Data...';
  Thread := MyThread.Create(True);
  Thread.FreeOnTerminate := True;
  Thread.Execute;
  Thread.Terminate;
  Thread.Free;
  // Verarbeitung der HTML Seite
  AoS_Namen := extractDataFromHTMl(sHttpText); // Seite Laden
  SetLength(AoT_SongItems, length(Aos_Namen));
  for i := 0 to length(AoS_Namen)-1 do begin
    sTemp := AoS_Namen[i];
    //iPosBegin := pos('nbsp;',sTemp);
    //iPosEnd := pos('</a>',sTemp);
    //(sTemp,1,9);
    //(sTemp,length(sTemp)-4,4);
    sTemp := copy(sTemp,9,length(sTemp)-12);
    sTemp := StringReplace(sTemp, '&amp;', 'and', [rfReplaceAll]);
    sTemp := StringReplace(sTemp, ';', '', [rfReplaceAll]);
    AoT_SongItems[i].sArtistTrack := sTemp;
    LItem := ListView1.Items.Add;
    LItem.Text := AoT_SongItems[i].sArtistTrack;
  end;
end;


procedure TForm1.Button2Click(Sender: TObject);
var
  Intent: JIntent;
begin
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  Intent.setData(StrToJURI('http://m.youtube.com/results?search_query=' + ListView1.Selected.Text));
  SharedActivity.startActivity(Intent);
  AoT_SongItems[ListView1.Selected.Index].bYoutoubed := true;
  Form1.Refresh(self);
end;

procedure TForm1.Checkbox_BuyChange(Sender: TObject);
begin
  AoT_SongItems[ListView1.Selected.Index].bBuy := Form1.Checkbox_Buy.IsChecked;
end;

procedure TForm1.Checkbox_MaybeChange(Sender: TObject);
begin
  AoT_SongItems[ListView1.Selected.Index].bMaybeBuy := Form1.Checkbox_Maybe.IsChecked;
end;

procedure TForm1.Checkbox_YoutubedChange(Sender: TObject);
begin
  AoT_SongItems[ListView1.Selected.Index].bYoutoubed := Form1.Checkbox_Youtubed.IsChecked;
end;

procedure TForm1.ListView1Change(Sender: TObject);

begin
  Form1.Refresh(self);
end;

procedure TForm1.Refresh(Sender: TObject);

begin
  Form1.Text1.Text := ListView1.Selected.Text;
  Checkbox_Youtubed.IsChecked := AoT_SongItems[ListView1.Selected.Index].bYoutoubed;
  Checkbox_Buy.IsChecked := AoT_SongItems[ListView1.Selected.Index].bBuy;
  Checkbox_Maybe.IsChecked := AoT_SongItems[ListView1.Selected.Index].bMaybeBuy;
end;

end.
